#include <cstdlib>
#include <fstream>
#include <iostream>

using std::cout;
using std::endl;
using std::cin;
using std::fstream;
using std::istream;

void printStreamStatus(istream & is)
{
    cout << "is's goodbit:" << is.good() << endl;
    cout << "is's badbit:" << is.bad() << endl;
    cout << "is's failbit:" << is.fail() << endl;
    cout << "is's eofbit:" << is.eof() << endl << endl;
}

int main()
{
    //fstream要求文件必须存在
    fstream fs("2.txt");
    if(!fs.good()) {
        cout << "fstream open file error" << endl;
        return EXIT_FAILURE;
    }

    cout << "111" << endl;
    int num = 0;
    //先从键盘获取数据，再写入到文件流中
    for(int i = 0; i < 5; ++i) {
        cin >> num;
        fs << num << " ";
    }
    cout << "num:" << num << endl;
    int pos = fs.tellg();
    cout << "pos:" << pos << endl;
    fs.seekg(0);//在读取数据之前，先进行文件游标的偏移

    printStreamStatus(fs);
    //从文件流中读取数据
    for(int i = 0; i < 5; ++i) {
        fs >> num;
        printStreamStatus(fs);
        cout << "num:" << num << endl;
    }

    fs.close();
    return 0;
}

